<?php 
//Rquire MySqlAdapter.
require_once(dirname(__FILE__).'/../db/MySqlAdapter.php');

//echo "<br>Step 1: open a connection to the DB<br>";
$conn = MySqlAdapter::connect1();


if (isset($_REQUEST['act']) && $_REQUEST['act']=="add"){
	if (isset($_REQUEST['equipment_name']) && isset($_REQUEST['description']))
	{
		if (($_REQUEST['equipment_name']) )
		{
			if ($conn):
				$result = MySqlAdapter::connect2();
			else:
				die ("Could not connect to db " . mysql_error());
			endif;

			//echo "<br>Step 2: update reservation of equipments table<br>";
			$query = "SELECT * FROM `equipment` WHERE `name`='".$_REQUEST['equipment_name']."';";
			$result = mysql_query($query) or die ("Invalid select " . mysql_error());
			if( mysql_fetch_array($result)){
				echo "The item is in the system already. No need to add it.";
			} else{
				$query = "INSERT INTO `ohel_shelomo_2012`.`equipment` (`id`, `name`, `description`) VALUES (NULL, '".$_REQUEST['equipment_name']."', '".$_REQUEST['description']."'); ";
				mysql_query($query) or die ("Invalid insert " . mysql_error());
				echo "The new item was added";				
			 }
		}
		else
			{ echo "Please fill all the fileds.";}
	} else
			{ echo "Please fill all the fileds. Or enter this page in the acceptable way";}
} elseif(isset($_REQUEST['act']) && $_REQUEST['act']=="remove"){
	if ( isset($_REQUEST['searchEquipment']) ){
		if ( $_REQUEST['searchEquipment'] ){		
			$query = "DELETE FROM `ohel_shelomo_2012`.`equipment` WHERE `equipment`.`name` = '".$_REQUEST['searchEquipment']."';";
			$result = mysql_query($query) or die ("Invalid Delete " . mysql_error());
			echo "The item ".$_REQUEST['searchEquipment']." was deleted !";
		}else{ echo "Please fill all the fileds.";}
	}else { echo "Please fill all the fileds. Or enter this page in the acceptable way";}
}
mysql_close($conn);
?>
